package service;

import dao.mongo.UserRepository;
import entity.mongo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Flux;

import java.util.ArrayList;
import java.util.List;

@Service
public class MongoService {

    @Autowired
    private UserRepository userRepository;

    public Flux<User> userList() {
//        Sort.Order
        List<String> orderList = new ArrayList<>();
        orderList.add("createTime");
//        new Sort(Sort.Direction.DESC, orderList);
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
//        sort.
        Example<User> of = Example.of(new User());
        
//        testRepository.findAll()
        return null;
    }

    @Transactional
    public Flux<User> addUser(List<User> list) {

        if (list == null || list.isEmpty()) {
            return null;
        }
        Flux<User> userFlux = userRepository.saveAll(list);
        if (list.size() % 2 == 1) {
            throw new RuntimeException();
        }
        return userFlux;
    }



}
